<!DOCTYPE html>

<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title> Chaincode Designer</title>
	<script src="blockly/blockly_compressed.js"></script>
	<script src="blockly/blocks_compressed.js"></script>
	<script src="blockly/msg/js/en.js"></script>
	<script src="blockly/go_compressed.js"></script>
	<script src='blockly/solidity_compressed.js'></script>
	<script src="blockly/natural_compressed.js"></script>
	<script src="blockly/blocks/chaincode.js"></script>
	<!--
		<script src="blockly/blocks/loop.js"></script>
	-->
	
	<link href="blockly/blockly_css.css" rel="stylesheet">
</head>

<body>
	
	<div class="firstline" onload="hello();"> Chaincode Designer beta V0.0.4
		<button onClick="deleteWorkspace()" id="trashButton" class="space"> <img src='blockly/media/1x1.gif' class="trash icon21"> </button>
		<button id="linkButton" class="space"> <img src='blockly/media/1x1.gif' class="link icon21"> </button>
		<button onClick="loadWorkspace()" class="space">Reload Blocks</button>
		<button onClick="saveWorkspace()" class="space">Save Blocks</button>
		<button onClick="saveXML()" class="space">SaveXML</button>
		<button onClick="files.click()" class="space">LoadXML</button>
		<input type="file" id="files" onChange="loadXML()" name="myFile" accept="application/xml" style="position:absolute; filter:alpha(opacity=0); opacity:0; width:30px; " />
		<button onClick="saveGO()" class="space">SaveGo</button>
		<button onClick="Blockly.mainWorkspace.undo()" class="space">Undo</button>
		<button onClick="Blockly.mainWorkspace.undo(true)" class="space">Redo</button>
	</div>
	<div class="app-container">
		<div id="blocklyDiv" class="main blockly-panel" style="width:55%"></div>
		<div id="codeDiv" class="main output-panel"style="width:42.5%;height:96.5%" >
			<div class="dropdown-bar"> Language:
				<div id="im-just-an-underline">
					<select id="languageDropdown" class="languageDropdown" onChange="myUpdateFunction();">
						<option value="Go" selected="selected">Go</option>
						<option value="Solidity">Solidity</option>
						<option value="Natural">Natural</option>
					</select>
				</div>
			</div>
			<hr class="POps">
			<pre></pre>
		</div>
		<div id="codeDiv2" class="main output-panel-natural"style="display: none;">
			<div class="dropdown-bar"> Language:
				<div id="im-just-an-underline">
					<select id="languageDropdown2" class="languageDropdown" onChange="myUpdateFunction();">
						<option value="Go">Go</option>
						<option value="Solidity">Solidity</option>
						<option value="Natural" selected="selected">Natural</option>
					</select>
				</div>
			</div>
			<hr class="POps">
			<pre></pre>
		</div>
	</div>
	<div style="display:none" id="playButton" class="play-button material-icons">Run</div>
	<xml id="toolbox" style="display: none">
		<category name=" 初始化 " colour="100" expanded="false">
			<block type="chaincode_init">
				<next>
					<shadow type="chaincode_body"> </shadow>
				</next>
			</block>
		</category>
		<category name=" 主体 " colour="200">
			<block type="chaincode_body"></block>
		</category>
		<sep gap="32"></sep>
		
		<category name=' 变量' colour='135'>
			<block type="math_number"></block>
		</category>

		<category id="catVariables" colour="245" custom="VARIABLE" name=" 组件">
		</category>
		<category name=" 函数" colour="300">
			<block type="chaincode_invoke"></block>
			<block type="chaincode_query"></block>
			<block type="chaincode_delete"></block>
			<block type="set_value"></block>
			<category name=" 创建函数" colour='250' custom='PROCEDURE'></category>
		</category>
		<sep></sep>
		<category colour="331" name=" 合约样例">
			<category name="查询 ">
				<block type="chaincode_init">
					<statement name="init_func">
						<block type="set_value">
							<field name="data">people</field>
							<value name="VARIABLE">
								<shadow type="math_number">
									<field name="NUM">20</field>
								</shadow>
							</value>
						</block>
					</statement>
					<next>
						<block type="chaincode_body">
							<statement name="body_func">
								<block type="chaincode_query">
									<value name="user_Query">
										<shadow type="variables_get">
											<field name="VAR">A</field>
										</shadow>
									</value>
								</block>
							</statement>
						</block>
					</next>
				</block>
			</category>
			<category name="交易">
				<block type="chaincode_init">
					<statement name="init_func">
						<block type="set_value">
							<field name="data">A</field>
							<value name="VARIABLE">
								<shadow type="math_number">
									<field name="NUM">100</field>
								</shadow>
							</value>
							<next>
								<block type="set_value">
									<field name="data">B</field>
									<value name="VARIABLE">
										<shadow type="math_number">
											<field name="NUM">200</field>
										</shadow>
									</value>
								</block>
							</next>
						</block>
					</statement>
					<next>
						<block type="chaincode_body">
							<statement name="body_func">
								<block type="chaincode_invoke">
									<value name="user_A">
										<shadow type="variables_get">
											<field name="VAR">A</field>
										</shadow>
									</value>
									<value name="user_B">
										<shadow type="variables_get">
											<field name="VAR">B</field>
										</shadow>
									</value>
									<value name="money_num">
										<shadow type="math_number">
											<field name="NUM">50</field>
										</shadow>
									</value>
									<next>
										<block type="chaincode_query">
											<value name="user_Query">
												<shadow type="variables_get">
													<field name="VAR">A</field>
												</shadow>
											</value>
											<next>
												<block type="chaincode_delete">
													<value name="user_Delete">
														<shadow type="variables_get">
															<field name="VAR">B</field>
														</shadow>
													</value>
												</block>
											</next>
										</block>
									</next>
								</block>
						</block>
					</next>
					</statement>
				</block>
			</category>
			

			<category name="发行token">
				<block type="chaincode_init">
					<statement name="init_func">
						<block type="chaincode_query">
							<value name="user_Query">
								<shadow type="variables_get">
									<field name="VAR">Bank</field>
								</shadow>
							</value>
							<next>
								
								<block type="chaincode_query">
									<value name="user_Query">
										<shadow type="variables_get">
											<field name="VAR">A</field>
										</shadow>
									</value>
								<next>
									<block type="set_value">
										<field name="data">temp</field>
										<value name="VARIABLE">
											<shadow type="math_number">
												<field name="NUM">0</field>
											</shadow>
										</value>
									</block>
								</next>
							</next>
						</block>
					</statement>
					<next>
						<block type="chaincode_body">
							<statement name="body_func">
								<block type="chaincode_invoke">
									<value name="user_A">
										<shadow type="variables_get">
											<field name="VAR">Bank</field>
										</shadow>
									</value>
									<value name="user_B">
										<shadow type="variables_get">
											<field name="VAR">temp</field>
										</shadow>
									</value>
									<value name="money_num">
										<shadow type="math_number">
											<field name="NUM">50</field>
										</shadow>
									</value>
									<next>
										<block type="chaincode_invoke">
											<value name="user_B">
												<shadow type="variables_get">
													<field name="VAR">temp</field>
												</shadow>
											</value>
											<value name="user_A">
												<shadow type="variables_get">
													<field name="VAR">A</field>
												</shadow>
											</value>
											<value name="money_num">
												<shadow type="math_number">
													<field name="NUM">50</field>
												</shadow>
											</value>
											<next>
												<block type="chaincode_query">
													<value name="user_Query">
														<shadow type="variables_get">
															<field name="VAR">A</field>
														</shadow>
													</value>
													<next>
														<block type="chaincode_delete">
															<value name="user_Delete">
																<shadow type="variables_get">
																	<field name="VAR">temp</field>
																</shadow>
															</value>
														</block>
													</next>
												</block>
											</next>
										</block>
									</next>
								</block>
						</block>
					</next>
					</statement>
				</block>
			</category>
		</category>
	</xml>
	<xml id="blocklyDefault" style="display: none">
		<block type="chaincode_init">
			<statement name="init_func">
				<block type="set_value">
					<field name="data">A</field>
					<value name="VARIABLE">
						<block type="math_number">
							<field name="NUM">500</field>
						</block>
					</value>
					<next>
						<block type="set_value">
							<field name="data">B</field>
							<value name="VARIABLE">
								<block type="math_number">
									<field name="NUM">1000</field>
								</block>
							</value>
						</block>
					</next>
				</block>
			</statement>
			<next><block type="chaincode_init">
					<statement name="init_func">
						<block type="set_value">
							<field name="data">A</field>
							<value name="VARIABLE">
								<shadow type="math_number">
									<field name="NUM">100</field>
								</shadow>
							</value>
							<next>
								<block type="set_value">
									<field name="data">B</field>
									<value name="VARIABLE">
										<shadow type="math_number">
											<field name="NUM">200</field>
										</shadow>
									</value>
								</block>
							</next>
						</block>
					</statement>
					<next>
						<block type="chaincode_body">
							<statement name="body_func">
								<block type="chaincode_invoke">
									<value name="user_A">
										<shadow type="variables_get">
											<field name="VAR">A</field>
										</shadow>
									</value>
									<value name="user_B">
										<shadow type="variables_get">
											<field name="VAR">B</field>
										</shadow>
									</value>
									<value name="money_num">
										<shadow type="math_number">
											<field name="NUM">50</field>
										</shadow>
									</value>
									<next>
										<block type="chaincode_query">
											<value name="user_Query">
												<shadow type="variables_get">
													<field name="VAR">A</field>
												</shadow>
											</value>
											<next>
												<block type="chaincode_delete">
													<value name="user_Delete">
														<shadow type="variables_get">
															<field name="VAR">B</field>
														</shadow>
													</value>
												</block>
											</next>
										</block>
									</next>
								</block>
						</block>
					</next>
					</statement>
				</block>
				<block type="chaincode_body">
					<statement name="body_func">
						<block type="chaincode_invoke">
							<value name="user_A">
								<shadow type="variables_get">
									<field name="VAR">A</field>
								</shadow>
							</value>
							<value name="user_B">
								<shadow type="variables_get">
									<field name="VAR">B</field>
								</shadow>
							</value>
							<value name="money_num">
								<shadow type="math_number">
									<field name="NUM">50</field>
								</shadow>
							</value>
							<next>
								<block type="chaincode_query">
									<value name="user_Query">
										<shadow type="variables_get">
											<field name="VAR">A</field>
										</shadow>
									</value>
									<next>
										<block type="chaincode_delete">
											<value name="user_Delete">
												<shadow type="variables_get">
													<field name="VAR">B</field>
												</shadow>
											</value>
										</block>
									</next>
								</block>
							</next>
						</block>
				</block>
			</next>
			</statement>
		</block>
	</xml>
	<script>
		//  var workspace = Blockly.inject('blocklyDiv',
		//      {toolbox: document.getElementById('toolbox')});
		// Construct the toolbox XML.
		var toolboxText = document.getElementById('toolbox').outerHTML;
		var toolboxXml = Blockly.Xml.textToDom(toolboxText);
		var workspace = Blockly.inject('blocklyDiv', {
			grid: {
				spacing: 25,
				length: 0,
				colour: '#0000',
				snap: true
			},
			media: 'blockly/media/',
			toolbox: toolboxXml,
			zoom: {
				controls: true,
				wheel: true
			},
			//sounds: false,
			//rtl: true,
			//trashcan: false
		});
		var defaultBlocks = document.getElementById('blocklyDefault');
		Blockly.Xml.domToWorkspace(defaultBlocks, workspace);
		workspace.scrollCenter();
		function myUpdateFunction(event) {
			var languageDropdown = document.getElementById('languageDropdown');
			var languageSelection = languageDropdown.options[languageDropdown.selectedIndex].value;
			var codeDiv = document.getElementById('codeDiv');
			var codeHolder = document.createElement('pre');
			codeHolder.id = 'code1';
			codeHolder.className = 'prettyprint but-not-that-pretty';
			var code = document.createTextNode(Blockly[languageSelection].workspaceToCode(workspace));
			codeHolder.appendChild(code);
			codeDiv.replaceChild(codeHolder, codeDiv.lastElementChild);
		}
		workspace.addChangeListener(myUpdateFunction);

		document.getElementById("linkButton").addEventListener("click", function() {
			workspace.scrollCenter();
		});

		function executeBlockCode() {
			window.alert("waiting completement");
		}
		document.getElementById("playButton").addEventListener("click", executeBlockCode);

		function saveXML() {
			var xmlDom = Blockly.Xml.workspaceToDom(Blockly.mainWorkspace);
			var contents = Blockly.Xml.domToPrettyText(xmlDom);
			window.alert(contents);
			var fileType = 'xml';
			var filename = prompt('Enter the file name under which to save your blocks.', 'test.xml');
			// Download file if all necessary parameters are provided.
			if(filename) {
				var data = new Blob([contents], {
					type: 'text/' + fileType
				});
				var clickEvent = new MouseEvent("click", {
					"view": window,
					"bubbles": true,
					"cancelable": false
				});

				var a = document.createElement('a');
				a.href = window.URL.createObjectURL(data);
				a.download = filename;
				a.textContent = 'Download file!';
				a.dispatchEvent(clickEvent);
				window.alert("保存成功");
			} else {
				alert('Could not export Blocks without file name under which to ' +
					'save blocks.');
			}
		}

		function saveGO() {
			//window.alert("123");
			//var code = document.createTextNode(Blockly['Go'].workspaceToCode(workspace));
			var code = document.getElementById('code1');
			var content = code.innerHTML;
			window.alert(content);
			var fileType = 'go';
			var filename = prompt('Enter the file name under which to save your go code.', 'test.go');
			// Download file if all necessary parameters are provided.
			if(filename) {
				var data = new Blob([content], {
					type: 'text/' + fileType
				});
				var clickEvent = new MouseEvent("click", {
					"view": window,
					"bubbles": true,
					"cancelable": false
				});

				var a = document.createElement('a');
				a.href = window.URL.createObjectURL(data);
				a.download = filename;
				a.textContent = 'Download code file!';
				a.dispatchEvent(clickEvent);
				window.alert("保存成功");
			} else {
				alert('Could not export Blocks without file name under which to ' +
					'save go code.');
			}
		}

		function loadXML() {
			//window.alert("打开成功");
			var files = document.getElementById('files');
			// If the file list is empty, the user likely canceled in the dialog.
			if(files.files.length > 0) {
				// The input tag doesn't have the "multiple" attribute
				// so the user can only choose 1 file.
				var file = files.files[0];
				var fileReader = new FileReader();
			}
			fileReader.addEventListener('load', function(event) {
				var fileContents = event.target.result;
				// Create empty object to hold the read block library information.
				//window.alert(fileContents);
				if(fileContents) {
					Blockly.mainWorkspace.clear();
					xmlDom = Blockly.Xml.textToDom(fileContents);
					Blockly.Xml.domToWorkspace(Blockly.mainWorkspace, xmlDom);
					window.alert("打开成功");
				}
			});
			fileReader.readAsText(file);
		}

		function saveWorkspace() {
			var xmlDom = Blockly.Xml.workspaceToDom(Blockly.mainWorkspace);
			var xmlText = Blockly.Xml.domToPrettyText(xmlDom);

			localStorage.setItem("blockly.xml", xmlText);
			window.alert("保存成功");
		}

		function loadWorkspace() {
			var xmlText = localStorage.getItem("blockly.xml");
			//window.alert(xmlText);
			if(xmlText) {
				Blockly.mainWorkspace.clear();				xmlDom = Blockly.Xml.textToDom(xmlText);
				Blockly.Xml.domToWorkspace(Blockly.mainWorkspace, xmlDom);
				window.alert("打开成功");
			}
		}

		function deleteWorkspace() {Lab
			var count = workspace.getAllBlocks().length;
			if(count < 2 ||
				window.confirm(Blockly.Msg.DELETE_ALL_BLOCKS.replace('%1', count))) {
				workspace.clear();
				if(window.location.hash) {
					window.location.hash = '';
				}
			}

			//onClick="workspace.scrollCenter()" 居中
			//onClick="workspace.zoomCenter(1)" 放大
			//onClick="workspace.zoomCenter(-1)" 缩小
			//<div id="playButton" class="play-button material-icons">run</div>
		};
	</script>
	<script>
		alert("欢迎使用智能合约编辑器BETA版\n本工具初步支持智能合约的可视化生成和简单的漏洞检测\n暂时支持Go语言和自然语言版本,Solidity版本仍在完善\n ");
	</script>
	<div class="lastline">  Developer: 徐霄雯 李珏宇 付博 &nbsp;&nbsp;&nbsp;&nbsp; | &nbsp;&nbsp; Producer: 陈建海 王备 黄步添 &nbsp;&nbsp;&nbsp;&nbsp; | &nbsp;&nbsp;Special thanks to: 蔡庆鹏 &nbsp;&nbsp;&nbsp;&nbsp; | &nbsp;&nbsp; Laboratory: ZJU-INCAS </div>
</body>
